package com.scau.meetingbook.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.scau.meetingbook.pojo.Users;
import com.scau.meetingbook.mapper.UsersMapper;
import com.scau.meetingbook.service.UsersService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.scau.meetingbook.vo.ResultVO;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;


import java.util.Date;
import java.util.List;

@Service
public class UsersServiceImpl extends ServiceImpl<UsersMapper, Users> implements UsersService {

    @Autowired
    private UsersMapper usersMapper;

    @Override
    public ResultVO insertUsers(Users users) throws Exception {
        users.setCreateBy(1);
        users.setCreateTime(new Date());
        usersMapper.insert(users);
        return ResultVO.success(null);
    }

    @Override
    public ResultVO<Users> getUsersById(Integer id) throws Exception {
        return ResultVO.success(null, usersMapper.selectById(id));
    }

    @Override
    public ResultVO updateUsersById(Users users) throws Exception {
        users.setUpdateTime(new Date());
        users.setUpdateBy(1);
        usersMapper.updateById(users);
        return ResultVO.success(null);
    }

    @Override
    public ResultVO deleteUsersById(Integer id) throws Exception {
        usersMapper.deleteById(id);
        return ResultVO.success(null);
    }

    @Override
    public ResultVO register(Users users) throws Exception {
        Users user = usersMapper.selectOne(new QueryWrapper<Users>()
                .eq("name", users.getName()));
        if (user != null) {
            return ResultVO.fail("用户已存在");
        }

        users.setStatus("pending");
        users.setCreateTime(new Date());
        usersMapper.insert(users);
        return ResultVO.success(null);
    }

    @Override
    public ResultVO login(Users users) throws Exception {
        Users user = usersMapper.selectOne(new QueryWrapper<Users>()
                .eq("name", users.getName()));
        if (user == null) {
            return ResultVO.fail("用户不存在");
        }

        if (!user.getLoginPassword().equals(users.getLoginPassword())) {
            return ResultVO.fail("密码错误");
        }
        if (user.getStatus().equals("frozen")) {
            return ResultVO.fail("用户已冻结");
        }
        return ResultVO.success("", user);
    }
    @Override
    public ResultVO changeUserStatus(Integer id, String status) throws Exception {
        Users user = usersMapper.selectById(id);
        if (user == null) {
            return ResultVO.fail("用户不存在");
        }
//        if (!user.getStatus().equals("active")) {
//            return ResultVO.fail("用户状态异常");
//        }
        user.setStatus(status);
        user.setUpdateBy(1);
        user.setUpdateTime(new Date());
        usersMapper.updateById(user);
        return ResultVO.success("成功", user);
    }

    @Override
    public ResultVO<Page<Users>> getUsers(Integer pageNum, Integer pageSize) {
        Page<Users> page = new Page<>(pageNum, pageSize);
        QueryWrapper<Users> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("create_time");
        usersMapper.selectPage(page, queryWrapper);
        return ResultVO.success("成功", page);
    }

    @Override
    public ResultVO<Page<Users>> getUsersByCondition(Users  users,  Integer pageNum, Integer pageSize) {
        if(pageNum == null || pageNum < 1)
            pageNum = 1;
        if(pageSize == null || pageSize < 1)
            pageSize = 10;
        Page<Users> page = new Page<>(pageNum, pageSize);
        QueryWrapper<Users> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(users.getName() != null, "name", users.getName());
        queryWrapper.like(users.getCompanyName() != null, "company_name", users.getCompanyName());
        queryWrapper.like(users.getPhoneNumber() != null, "phone_number", users.getPhoneNumber());
        queryWrapper.like(users.getStatus() != null, "status", users.getStatus());
        usersMapper.selectPage(page, queryWrapper);
        return ResultVO.success("成功", page);
    }
}